


insert into UE_ASP_RIGHEFATTURAGAS_app1

 

(SELECT rg.gf_id, vfl.valfl_valdis, fr.catcomser_des, fr.scl_des,

        fr.comser_des, fr.ot_des, fr.voce_des, fr.rvv_umidqta1,

        fr.rvv_umidqta2, fr.rvv_qta2, fr.fatvoceot_imp, fr.fatvoceot_pu,

        fr.fatvoceot_qta, fr.iva_id, TRUNC (fr.ver_dtini) data_inizio,

        TRUNC (fr.ver_dtfine) data_fine, fr.lis_des,

        fr.tipo_consumo tipo_consumo, fr.rvv_flstato tipo_calcolo, rg.ragf_id,

        fr.voce_id, fr.sfat_id, fr.rvv_num, fr.rfat_id, ft.tfat_id,

        ftr.rfat_sgn,FR.CUF_ID, FT.FAT_DT

   FROM sic4u_ambue.v_fatturar fr,

        sic4u_ambue.fatturat ft,

        sic4u_ambue.v_ragfat rg,

        sic4u_ambue.valflag vfl,

        sic4u_ambue.fatturar ftr,

        UE_ASP_RIGHEFATTURAGAS_TFATID TT

  WHERE fr.az_id = ft.az_id

    AND fr.tfat_id = ft.tfat_id

    AND ft.regivapro_id = 1

    AND (SELECT CASE

                   WHEN ista.iss_staid <> 0

                   AND ista.iss_staidsuper = 0

                   AND ista.iss_sstaidsuper = 0

                      THEN ista.iss_staid

                   WHEN ista.iss_staid <> 0

                   AND ista.iss_staidsuper <> 0

                   AND ista.iss_sstaidsuper = 0

                      THEN ista.iss_staidsuper

                   WHEN ista.iss_staid <> 0

                   AND ista.iss_staidsuper <> 0

                   AND ista.iss_sstaidsuper <> 0

                      THEN ista.iss_sstaidsuper

                   ELSE -1

                END

           FROM sic4u_ambue.ista ista

          WHERE ista.az_id = ft.az_id

            AND ista.csi_id = ft.csi_id

            AND ista.ista_verstato = ft.ista_verstato) IN (01,14)

    AND ft.az_id = rg.az_id

    AND ft.ragf_id = rg.ragf_id

    AND vfl.flag_id = 'FAT_FLTIPO'

    AND vfl.valfl_val = ft.fat_fltipo

    AND fr.az_id = ftr.az_id(+)

    AND fr.rfat_id = ftr.rfat_id(+)

    AND fr.voce_id NOT IN (83, 84)
   --and FR.CUF_ID=10181
        and FT.TFAT_ID=tt.tfat_id)
